/*
 * @lc app=leetcode.cn id=337 lang=javascript
 *
 * [337] 打家劫舍 III
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var rob = function(root) {
    let Has=new Map()
    let notHas=new Map()
    let dfs=function(node){
        if(!node)return 
        dfs(node.left)
        dfs(node.right)
        Has.set(node,node.val+(notHas.get(node.left)||0)+(notHas.get(node.right)||0))
        notHas.set(node, Math.max(Has.get(node.left) || 0, notHas.get(node.left) || 0) + Math.max(Has.get(node.right) || 0, notHas.get(node.right) || 0));
        
    }
    dfs(root)
    return Math.max(Has.get(root),notHas.get(root))//一定记住中序遍历，因为父节点由子节点的值确定
};
// @lc code=end

